#include "tcpconnection.h"
#include "asyncservice.h"
#include "tcpconnectionfactory.h"
#include <boost/bind.hpp>

TcpConnectionFactory::TcpConnectionFactory(AsyncService &service) :
    m_service(service)
{

}

TcpConnectionFactory::~TcpConnectionFactory()
{

}

TcpConnectionFactory::ConnectionPtr TcpConnectionFactory::create()
{
    ConnectionPtr _new;

    void *p = m_mp.alloc(sizeof(TcpConnection));

    if(!p){
        return _new;
    }

    _new = ConnectionPtr
            (
                new(p) TcpConnection(m_service.implement()),
                boost::bind
                (
                    &TcpConnectionFactory::destroy,
                    this,
                    _1
                )
            );

    return _new;
}

void TcpConnectionFactory::destroy(TcpConnection *c)
{
    if(!c){
        return;
    }

    c->~TcpConnection();

    m_mp.free(c, sizeof(TcpConnection));
}
